using System;
using System.Windows.Forms;
using OhioState.Graphics;
using System.IO;


namespace OhioState.Controls
{
    public partial class VolumeDim : Form
    {
        #region Public Properties
        public int XDim { get; private set; }
        public int YDim { get; private set; }
        public int ZDim { get; private set; }
        public TexelFormat TexelType { get; private set; }
        #endregion

        public VolumeDim(string fileName)
        {
            FileInfo fi = new FileInfo(fileName);
            if (fi != null)
                fileSize = fi.Length;

            InitializeComponent();
            voxelTypeComboBox.SelectedIndex = 1;
            xSizeUpDown1.Value = 256;
            ySizeUpDown2.Value = 256;
            zSizeUpDown3.Value = 256;
        }
        
        private void button1_Click(object sender, EventArgs e)
        {
            XDim = (int)xSizeUpDown1.Value;
            YDim = (int)ySizeUpDown2.Value;
            ZDim = (int)zSizeUpDown3.Value;
            TexelFormat texelType = new TexelFormat();
            texelType.Format = ImageFormat.ALPHA;
            texelType.Type = (ByteFormat)Enum.Parse(typeof(ByteFormat),voxelTypeComboBox.Text,true);
            TexelType = texelType;
            this.DialogResult = DialogResult.OK;
            this.Close();
        }

        private void VolumeDim_Load(object sender, EventArgs e)
        {
            Int64 size = (long)System.Math.Round(System.Math.Pow(fileSize, 0.3333333));
            xSizeUpDown1.Value = size;
            ySizeUpDown2.Value = size;
            zSizeUpDown3.Value = fileSize / (size*size);
            voxelTypeComboBox.SelectedIndex = 1;
        }

        private Int64 fileSize = 0;

        private void volumeSize_ValueChanged(object sender, EventArgs e)
        {
            Int64 size = fileSize / (long)(xSizeUpDown1.Value * ySizeUpDown2.Value);
            TexelFormat texelType = new TexelFormat();
            texelType.Format = ImageFormat.ALPHA;
            texelType.Type = (ByteFormat)Enum.Parse(typeof(ByteFormat),voxelTypeComboBox.Text,true);
            zSizeUpDown3.Value = size / texelType.FormatSize;
        }
    }
}
